<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <div style="background: #fff; padding: 13px 15px;">
            <span :class="[state.status == 0 ? 'btn btn-info' : 'btn']" @click="changeStatus(0)">{{ t('全部') }}</span>
            <span :class="[state.status == 1 ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeStatus(1)">{{ t('进行中') }}({{ state.sub_status.count_1 }})</span>
            <span :class="[state.status == 2 ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeStatus(2)">{{ t('已成交') }}({{ state.sub_status.count_2 }})</span>
            <span :class="[state.status == 3 ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeStatus(3)">{{ t('已终止') }}({{ state.sub_status.count_3 }})</span>
            <span :class="[state.status == 4 ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeStatus(4)">{{ t('内部') + '(' + t('完成') + ')' }}({{ state.sub_status.count_4 }})</span>
        </div>


        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['add', 'delete', 'quickSearch', 'columnDisplay']" :quick-search-placeholder="t('搜索')">
            
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">
            <template #name>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('提案')" align="center" min-width="120" class-name="business-name">
                    <template #default="scope">
                        <div>
                            <div class="tian" @click="clickTian(scope.row)">{{ scope.row.name }}</div>
                            <div class="btn btn-mini" style="margin-top: 5px;" @click="viewQuotation(scope.row)">{{ t('查看报价') }}({{ scope.row.quotation.length }})</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #customer_name>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('客户名')" align="center">
                    <template #default="scope">
                        <div>
                            <div v-if="scope.row.user_id > 0" class="tian" @click="clickCustomer(scope.row)">{{ scope.row.customer ? scope.row.customer.corp_name_ko : t('未填写客户名') }}</div>
                            <div v-else class="tian" @click="clickCustomer(scope.row)">{{ t('内部业务') }}</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #describe>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('内容')" align="center" width="360">
                    <template #default="scope">
                        <!-- <div style="text-align: left; ">
                            <pre style="word-break: break-all; word-wrap: break-word; white-space: pre-wrap;">{{ scope.row.describe }}</pre>
                        </div> -->
                        <el-tooltip :content="'<pre>' + scope.row.describe + '</pre>'" placement="right" :raw-content="true">
                            <div class="describe">
                                <pre>{{ scope.row.describe }}</pre>
                            </div>
                        </el-tooltip>

                        
                    </template>
                </el-table-column>
            </template>
            <template #status>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('操作')" align="center" width="150" class-name="business-name">
                    <template #default="scope">
                        <div v-if="scope.row.user_id > 0">
                            <div class="btn ml-0 mt-0 btn-small" @click="add(scope)">{{ t('添加') }}</div>
                            <div class="btn ml-5 mt-0 btn-small" @click="edit(scope)">{{ t('编辑') }}</div>
                            <div :class="[scope.row.status.includes('1') ? 'btn btn-info ml-0 mt-5 btn-small' : 'btn ml-0 mt-5 btn-small']" @click="setStatus(scope.row.no, 1, scope.row)">{{ t('成交') }}</div>
                            <div :class="[scope.row.status.includes('0') ? 'btn btn-danger mt-5 ml-5 btn-small' : 'btn mt-5 ml-5 btn-small']" @click="setStatus(scope.row.no, 0, scope.row)">{{ t('终止') }}</div>
                            
                            <el-popconfirm title="确定要删除这条数据吗?" @confirm="del(scope)">
                                <template #reference>
                                    <div class="btn ml-5 mt-5 btn-small">{{ t('删除') }}</div>
                                </template>
                            </el-popconfirm>
                        </div>
                        <div v-if="scope.row.user_id == 0">
                            <div class="btn ml-0 mt-0 btn-small" @click="add(scope)">{{ t('添加') }}</div>
                            <div v-if="scope.row.user_id == 0" :class="[scope.row.status.includes('1') ? 'btn btn-info mt-0 ml-5 btn-small' : 'btn mt-0 ml-5 btn-small']" @click="setStatus(scope.row.no, 1, scope.row)">{{ t('完成') }}</div>
                            <div class="btn ml-0 mt-5 btn-small" @click="edit(scope)">{{ t('编辑') }}</div>
                            <el-popconfirm title="确定要删除这条数据吗?" @confirm="del(scope)">
                                <template #reference>
                                    <div class="btn ml-5 mt-5 btn-small">{{ t('删除') }}</div>
                                </template>
                            </el-popconfirm>
                            <div :class="[scope.row.status.includes('0') ? 'btn btn-danger mt-5 ml-0 btn-small' : 'btn mt-5 ml-0 btn-small']" @click="setStatus(scope.row.no, 0, scope.row)">{{ t('终止') }}</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #files>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('附加文件')" align="center" min-width="100">
                    <template #default="scope">
                        <div style="max-height: 110px; overflow: auto; " class="fjwj">
                            <el-link :href="item.url" target="_blank" class="" style="margin-top: 5px; font-size: 12px;" v-for="(item, index) in scope.row.files" :key="index">{{ item.name }}</el-link>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #id>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('提案编号')" align="center" width="120">
                    <template #default="scope">
                        <div>No.{{ scope.row.no }}</div>
                        <!-- <div v-if="scope.row.status.indexOf('1') !== -1" class="btn btn-mini" style="margin-top: 5px;" @click="openInformation(scope.row)">{{ t('资料管理') }}</div> -->
                    </template>
                </el-table-column>
            </template>
            <template #create_time>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('时间')" align="center" width="100">
                    <template #default="scope">
                        <div style="">
                            <div>{{ scope.row.create_date_text }}</div>
                            <div>{{ scope.row.create_time_text }}</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />
        <AddProcess />

        <Quotation v-if="quotation.id != 0" :data="quotation" @closeViewQuotation="closeViewQuotation" />

        <!-- <ImportFrom v-if="!state.loading" v-model="state.showImportFrom" /> -->
        

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import AddProcess from './add_process.vue'
import Quotation from './quotation/index.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'


import { setStatusApi } from '/@/api/most/business/business'

// import ImportFrom from './import.vue'



defineOptions({
    name: 'business/business/index',
})

const { t } = useI18n()
const tableRef = ref()


const state = reactive({
    showImportFrom: false,
    loading: false,
    status: 0,
    
    sub_status: {
        count_1: 0,
        count_2: 0,
        count_3: 0,
        count_4: 0
    },
    hsCode: [],
    goods: [],
    goodsForm: {},
    hscodeFormData: [],
    hscodeForm: {},
    
});



const quotation = reactive({
    id: 0,
    name: '',
})

const edit = (scope: anyObj) => {
    baTable.onTableAction('edit', scope)
}

const add = (scope: anyObj) => {
    baTable.form.operate = 'AddProcess'
    baTable.form.items!.id = scope.row.id
    baTable.form.items!.name_cn = scope.row.name_cn;
    baTable.form.items!.name_ko = scope.row.name_ko;
}


const closeInformation = () => {
    state.information.form = {};
    state.information.drawer = false;
}

const del = (scope: anyObj) => {
    baTable.onTableAction('delete', scope)
}

const clickTian = (row: anyObj) => {
    baTable.table.filter!.no = row.no;
    baTable.table.filter!.customer_id = null;
    baTable.getIndex();
}

const clickCustomer = (row: anyObj) => {
    baTable.table.filter!.customer_id = row.user_id;
    baTable.table.filter!.no = null;
    baTable.getIndex();
}
/**
 * 切换状态
 */
const changeStatus = (status: number) => {
    state.status = status;
    baTable.table.filter!.status = status;
    baTable.table.filter!.no = 0;
    baTable.table.filter!.customer_id = null;
    baTable.getIndex();
}

const test = () => {
    console.log(baTable.table.filter)
}
/**
 * 设置状态 
 */
const setStatus = (no: number, status: number, row: anyObj) => {
    let data = {no: no, status: status}

    console.log(baTable.table.data); 

    setStatusApi(data).then((e) => {
        // row.status = e.data.status;
        state.sub_status = e.data.sum_status

        baTable.table.data?.forEach(item => {
            if(item.no == no){
                item.status = e.data.status;
            }
        });

    })
}


let optButtons = defaultOptButtons([])

/**
 * 查看报价
 * @param row 
 */
const viewQuotation = (row: anyObj) => {
    window.open('/system/business/quotation/' + row.no);
}
/**
 * 关闭
 */
const closeViewQuotation = () => {
    quotation.id = 0;
}

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: '编辑',
        type: 'primary',
        class: 'btn btn-primary',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: '删除',
        type: 'danger',
        class: 'btn btn-danger ml--6 mt-8',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    },{
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: '', // 按钮名称
        title: '', // 鼠标放置时的 title 提示
        class: 'btn mt-8',
        text: '添加进程', // 直接在按钮内显示的文字，title 有值时可为空
        type: 'info', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            baTable.form.operate = 'PlaceOrder';
            baTable.form.items!.id = row.id;
            baTable.form.items!.name = row.name;
            baTable.form.items!.unit = row.unit;
            baTable.form.items!.pay_type = -1;

        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return false;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            return false;
        },
        attr: {} // 自定义el-button属性
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])





/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/business.Business/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { 
                label: t('提案编号'), 
                prop: 'id', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE', 
                sortable: false, 
                render: 'slot',
                slotName: 'id'
            },
            { label: t('业务名称'), render: 'slot', slotName: 'name', prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('业务描述'), prop: 'describe', align: 'left', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false, render: 'slot', slotName: 'describe' },
            { 
                label: t('客户名'), 
                prop: 'customer.corp_name_ko', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE', 
                sortable: false,
                // render: 'string',
                // renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                //     return row.user_id > 0 && row.customer ? row.customer.corp_name_ko : t('内部业务');
                // },
                render: 'slot',
                slotName: 'customer_name'
            },
            { label: t('作者'), prop: 'admin.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('附加图片'), minWidth: 110, render: "images", prop: 'images', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('附加文件'), minWidth: 100, render: 'slot', slotName: 'files', prop: 'files', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            
            { label: t('添加时间'), render: "slot", prop: 'create_time', align: 'center', slotName: 'create_time' },
            { label: t('状态'), render: 'slot', slotName: 'status', prop: 'status', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('Operate'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 100 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: {  },
    },{
        
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后
            state.sub_status = res.data.sum_status;
            // console.log(res.data);
            // console.log(state.sub_status)
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">

.describe{
    display: -webkit-box; /* 用于支持 WebKit 浏览器 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 5; /* 限制显示5行 */
  max-height: 7.5em; /* 计算高度：5行 * 行高（假设行高为1.5em） */
  line-height: 1.5em; /* 设置行高 */
  text-align: left;
}

:deep(.el-image){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    // max-width: 80px;
    // max-height: 80px;
}
:deep(td.business-name > div.cell) {
    min-height: 80px;
    display:flex;
    justify-content:center; 
    align-items:center;
}
.tian{
    cursor: pointer;
}
.tian:hover{
    color: #409EFF;
}

:deep(.el-image > img){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    max-width: 80px;
    max-height: 80px;
}
:deep(.ba-data-table .btn){
    padding: 4px 14px;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
:deep(.pay-type .el-radio){
    margin-right: 10px;
}
pre{
    font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;
}
:deep(.ba-operate-dialog .el-dialog__body){
    padding-bottom: 0;
    padding-left: 0;
    padding-right: 0;
}
:deep(.quotation-scrollbar .el-scrollbar:first-child > .el-scrollbar__wrap){
    height: 100%;
}
:deep(.ml-0){
    margin-left: 0!important;
}
:deep(.mt-8){
    margin-top: 8px;
}
:deep(.ml--6){
    margin-left: -6px;
}
:deep(.ml-5){
    margin-left: 5px;
}
:deep(.mt-5){
    margin-top: 5px;
}
:deep(.ba-operate-dialog .el-dialog__body){
    // height: calc(90vh - 55px);
}
:deep(.el-table__row.no-hover:hover .no-hover) {
  background-color: transparent!important; /* 使用 !important 来确保覆盖默认样式 */
  color: inherit !important;
//   background-color: red;
}
.tag{
    margin-right: 18px;
}

// .tag .active{
//     background: #ECF5FF;
//     color: #409EFF;
//     border: 1px solid #C6E2FF;
// }
:deep(.information .el-drawer__body){
    padding-top: 0;
}
:deep(.upload-column .el-form-item__label){
    display: none;
}
:deep(.upload-column .ba-upload.file){
    line-height: 8px;
}
:deep(.tag-0 .el-table td.el-table__cell, .el-table th.el-table__cell.is-leaf){
    border-top: 2px solid #efefef;
    border-right: 2px solid #efefef;

}

:deep(.information .el-table th.el-table__cell){
    background: rgb(245, 245, 246);
    color: #333;
    border-color: rgb(234, 234, 234);
}
:deep(.information .el-form--inline .el-form-item){
    margin-right: 23px;
}
:deep(.information .el-timeline-item){
    padding-bottom: unset;
}
:deep(.information .el-timeline-item__timestamp){
    font-size: 14px;
}
:deep(.el-button){
    height: unset;
}

:deep(.el-image > img){
    max-width: 50px;
    max-height: 50px;
}
:deep(.el-table){
    height: calc(100vh - 275px);
}
:deep(.default-main){
    margin-bottom: 0!important;
}
.default-main{
    margin-bottom: 0!important;
}
:deep(.el-scrollbar__bar.is-horizontal){
    // display: block!important;
    // position: fixed;
    height: 14px;
    top: -56px;
}
:deep(.el-scrollbar__bar.is-horizontal .el-scrollbar__thumb){
    position: fixed;
    height: 16px;
}
@media (max-width: 1575px) {  
    :deep(.el-table){
        margin-top: 18px;
        height: calc(100vh - 293px);
    } 
}
</style>
